{% extends 'admin/base.html' %}
{% set active_page = 'member_publications' %}

{% block title %}会员发布管理{% endblock %}

{% block page_title %}会员发布管理{% endblock %}

{% block page_subtitle %}查看和管理会员发布的系统内置量表{% endblock %}

{% block content %}

<div class="content-scroll">
    <!-- 统计卡片 -->
    <div class="row mb-4">
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-primary">
                        <i class="bi bi-share" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="totalPublications">-</h3>
                        <small class="text-muted">总发布数</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-primary me-2" id="publicationGrowth">+0%</span>
                    <small class="text-muted">较上月增长</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-success">
                        <i class="bi bi-check-circle" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="activePublications">-</h3>
                        <small class="text-muted">活跃发布</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-success me-2" id="activeRate">0%</span>
                    <small class="text-muted">活跃率</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-info">
                        <i class="bi bi-eye" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="totalViews">-</h3>
                        <small class="text-muted">总访问量</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-info me-2" id="avgViews">0</span>
                    <small class="text-muted">平均访问</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-warning">
                        <i class="bi bi-clipboard-check" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="totalAssessments">-</h3>
                        <small class="text-muted">总测评数</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-warning me-2" id="avgAssessments">0</span>
                    <small class="text-muted">平均测评</small>
                </div>
            </div>
        </div>
    </div>

    <!-- 筛选和搜索 -->
    <div class="content-card mb-4">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">搜索</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="searchInput" placeholder="搜索量表标题或会员...">
                        <button class="btn btn-outline-secondary" type="button" onclick="searchPublications()">
                            <i class="bi bi-search"></i>
                        </button>
                    </div>
                </div>
                <div class="col-md-2">
                    <label class="form-label">状态</label>
                    <select class="form-select" id="statusFilter" onchange="loadPublications()">
                        <option value="">全部状态</option>
                        <option value="active">活跃</option>
                        <option value="inactive">非活跃</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">量表类型</label>
                    <select class="form-select" id="scaleFilter" onchange="loadPublications()">
                        <option value="">全部量表</option>
                        <!-- 动态加载量表选项 -->
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">排序方式</label>
                    <select class="form-select" id="sortBy" onchange="loadPublications()">
                        <option value="created_at">创建时间</option>
                        <option value="access_count">访问次数</option>
                        <option value="assessment_count">测评次数</option>
                        <option value="last_accessed_at">最后访问</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">操作</label>
                    <div class="d-flex gap-2">
                        <button class="btn btn-outline-primary" onclick="exportPublications()">
                            <i class="bi bi-download me-1"></i>导出
                        </button>
                        <button class="btn btn-outline-secondary" onclick="refreshData()">
                            <i class="bi bi-arrow-clockwise me-1"></i>刷新
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 发布列表 -->
    <div class="content-card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">发布列表</h5>
            <div class="d-flex align-items-center">
                <span class="text-muted me-3" id="publicationCount">共 0 个发布</span>
                <div class="btn-group btn-group-sm">
                    <button class="btn btn-outline-secondary active" title="列表视图">
                        <i class="bi bi-list"></i>
                    </button>
                    <button class="btn btn-outline-secondary" title="卡片视图">
                        <i class="bi bi-grid"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>
                                <input type="checkbox" class="form-check-input" id="selectAll">
                            </th>
                            <th>量表信息</th>
                            <th>发布会员</th>
                            <th>自定义信息</th>
                            <th>状态</th>
                            <th>访问统计</th>
                            <th>测评统计</th>
                            <th>发布时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="publicationsTableBody">
                        <tr>
                            <td colspan="9" class="text-center py-5">
                                <div class="spinner-border text-primary" role="status">
                                    <span class="visually-hidden">加载中...</span>
                                </div>
                                <p class="mt-2 text-muted">正在加载发布数据...</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- 分页 -->
        <div class="card-footer">
            <nav aria-label="发布列表分页">
                <ul class="pagination justify-content-center mb-0" id="pagination">
                    <!-- 分页将通过JavaScript动态生成 -->
                </ul>
            </nav>
        </div>
    </div>
</div>

<!-- 发布详情模态框 -->
<div class="modal fade" id="publicationDetailModal" tabindex="-1" aria-labelledby="publicationDetailModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="publicationDetailModalLabel">
                    <i class="bi bi-info-circle me-2"></i>发布详情
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body" id="publicationDetailContent">
                <!-- 详情内容将通过JavaScript动态加载 -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="viewPublicationAssessments()">查看测评记录</button>
            </div>
        </div>
    </div>
</div>

<!-- 状态管理模态框 -->
<div class="modal fade" id="statusModal" tabindex="-1" aria-labelledby="statusModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-warning text-dark">
                <h5 class="modal-title" id="statusModalLabel">
                    <i class="bi bi-gear me-2"></i>状态管理
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="statusForm">
                    <input type="hidden" id="statusPublicationId">
                    <div class="mb-3">
                        <label for="newStatus" class="form-label">新状态</label>
                        <select class="form-select" id="newStatus" required>
                            <option value="">请选择状态</option>
                            <option value="active">活跃</option>
                            <option value="inactive">非活跃</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="statusReason" class="form-label">操作原因</label>
                        <textarea class="form-control" id="statusReason" rows="3" placeholder="请输入状态变更原因..."></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-warning" onclick="updatePublicationStatus()">确认更新</button>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/admin-member-publications.js') }}"></script>
{% endblock %}